from pathlib import Path

from sklearn.linear_model import SGDClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split

from SimpleProcessor import Simple_Processor
from SimpleDataSetLoader import Simple_DataSet_Loader


def main():
    s_pre = Simple_Processor(32,  32)
    s_loader = Simple_DataSet_Loader(preprocessors=[s_pre])
    data, labels = s_loader.load([str(i) for i in Path("../../dataset/dog_cat").iterdir()])
    data = data.reshape((data.shape[0], 3072))
    le = LabelEncoder()
    labels = le.fit_transform(labels)
    train_x, test_x, train_y, test_y = train_test_split(data, labels,
                                                        test_size=0.25,
                                                        random_state=5)
    for r in (None, "l1", "l2"):
        print(f"[info]:正在使用正则化规则--{r if r else '无'}")
        model = SGDClassifier(loss="log",
                              penalty=r, max_iter=100, learning_rate="constant",
                              eta0=0.01, random_state=42)
        model.fit(train_x, train_y)

        acc = model.score(test_x, test_y)
        print(f"[info]:{r if r else '无'}正则化规则准确率--{acc * 100:.2f}%")


if __name__ == '__main__':
    main()
